<?php
class DvutsinhviensController extends AppController {

	var $name = 'Dvutsinhviens';
    var $uses = array('Dvutsinhvien','Phanloaisinhvien','Sinhvien','Z1phanloaisinhvien','Z1dvutsinhvien','Chidoansv','Z1dvutcb');
    /**
     * function index()
     * Loc ra danh sach doan vien xuat sac tu ds phan loai doan vien
     * Bi thu check lai ds doan vien uu tu, dien so nguoi dong y va so nguoi tham gia bau vao roi luu vao csdl
     * */
    
	function index($cd=null) {
        $duplicate="";
        $error="";
        $daduyet="";
        $this->Dvutsinhvien->recursive = -1;
        $namhocht_id=$this->Session->read('namhocht_id');
        $cd=$this->Session->read('chidoanht_id');
        //lay danh sach doan vien uu tu tu bang phan loai dv
        $dvutsinhviens=$this->Z1phanloaisinhvien->find('all', array(
            'conditions'=> array('xeploai_id'=>1,'namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cd)));
        //pr($dvutsinhviens);
        $tongso=$this->Z1phanloaisinhvien->find('count', array(
            'conditions'=> array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cd)));
        $this->set(compact('dvutsinhviens', 'tongso'));
        $data=$this->data;
        //pr($data);
        if (!empty($data)) {
            $songuoithamgiabau=trim($data['Dvutsinhvien']['songuoithamgiabau']);
            if($songuoithamgiabau==''||$songuoithamgiabau<=0){
                $this->Session->setFlash(__('Số người tham gia bầu phải lớn hơn 0', true));
            }elseif($songuoithamgiabau>$tongso){
                $this->Session->setFlash(__('Số người tham gia bầu phải nhỏ hơn tổng số đoàn viên của chi đoàn', true));
            }else{
                for($j=0;$j<(count($data['Dvutsinhvien'])-1)/4;$j++){ //tru di 1 vi bo di 2 phan tu trong mang, chia 3 la so luong form
                    $Dvutsinhvien=null;
                    if(trim($data['Dvutsinhvien']['songuoidongy'.$j])!='' && $data['Dvutsinhvien']['songuoidongy'.$j]<=$songuoithamgiabau&&$data['Dvutsinhvien']['songuoidongy'.$j]>0){
                        $Dvutsinhvien[]=array('sinhvien_id'=>$data['Dvutsinhvien']['sinhvien_id'.$j],
                                    'namhoc_id'=>$namhocht_id,
                                    'songuoidongy'=>$data['Dvutsinhvien']['songuoidongy'.$j],
                                    'songuoithamgiabau'=>$songuoithamgiabau,
                                    'ghichu'=>$data['Dvutsinhvien']['ghichu'.$j]
                        );
                        $dvut=$this->Dvutsinhvien->find('first',array('conditions'=>array('sinhvien_id'=>$data['Dvutsinhvien']['sinhvien_id'.$j],'namhoc_id'=>$namhocht_id),'recursive'=>-1));                               
            			if($dvut==null){
                            $this->Dvutsinhvien->create();
                            $this->Dvutsinhvien->saveAll($Dvutsinhvien);
                            $this->Dvutsinhvien->id=false;
            			}else if($dvut['Dvutsinhvien']['duyet']==1){
                    			 $daduyet=$daduyet.'MSSV: '.$data['Dvutsinhvien']['mssv'.$j].' đã được duyệt là đoàn viên ưu tú!!';
                                 $this->set('daduyet',$daduyet);}
                            else{
            			     $duplicate=$duplicate.'MSSV: '.$data['Dvutsinhvien']['mssv'.$j].' đã được bình xét đoàn viên ưu tú rồi!!';
                             $this->set('duplicate',$duplicate);
            			     }
                    }else if($data['Dvutsinhvien']['songuoidongy'.$j]>$songuoithamgiabau){
                        $error=$error.'MSSV: '.$data['Dvutsinhvien']['mssv'.$j].' có số người đồng ý lớn hơn số người tham gia bầu?</br>';
                        $this->set('error',$error);
                    } 
        	   }
            }
        }
	}
    /**
     * function viewdvut()
     * Xem danh sach doan vien uu tu cua chi doan hien tai, trong hoc ky hien tai sau khi da binh xet dvut 
     * */
    function viewdvut(){
        $namhocht_id=$this->Session->read('namhocht_id');
        $cd=$this->Session->read('chidoanht_id');
        $dvutsinhviens=$this->Z1dvutsinhvien->find('all', array('conditions'=> array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cd)));
       // pr($dvutsinhviens);
        $this->set('Dvutsinhviens',$dvutsinhviens);
    }
    /** exp mau4
     * 
     * /
     * */
     
    function reviewdvut(){
        $namhocht_id=$this->Session->read('namhocht_id');
        //$khoa_id=$this->Session->read('khoa_id');
        $namhoc_id=$this->Session->read('namhocht_id');
        $dvutsinhviens=$this->Z1dvutsinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id),'recursive'=>0));
        $dvutcbs=$this->Z1dvutcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id),'recursive'=>0));
        $this->set(compact('dvutsinhviens','dvutcbs'));
        //pr($dvutcbs);
    }
    function ketquadvut(){
        $this->layout = 'excel';
        $namhocht_id=$this->Session->read('namhocht_id');
        //$khoa_id=$this->Session->read('khoa_id');
        $namhoc_id=$this->Session->read('namhocht_id');
        $dvutsinhviens=$this->Z1dvutsinhvien->find('all',array('conditions'=>array('namhoc_id'=>$namhocht_id),'recursive'=>0));
        $dvutcbs=$this->Z1dvutcb->find('all',array('conditions'=>array('namhoc_id'=>$namhoc_id),'recursive'=>0));
        $this->set(compact('dvutsinhviens','dvutcbs'));
        //pr($dvutcbs);
    }
    /**
     * function danhsachcd()
     * Liet ke danh sach cac chi doan de duyet 
     * */
    function danhsachcd(){
        $chidoansvs=$this->Chidoansv->find('all',array('conditions'=>array('khoahoc_id <>'=>null)));
        $this->set(compact('chidoansvs'));
        $model='Chidoansv';
        if(!empty($this->data)){
            //pr($this->data);exit;
            if(trim($this->data['Dvutsinhvien']['data'])!=''){
                $results=$this->$model->find('all',array(
                                            'conditions'=>array(
                                                            'or'=>array(
                                                                $model.'.machidoansv Like '=>'%'.$this->data['Dvutsinhvien']['data']."%",
                                                                $model.'.tenchidoan Like '=>'%'.$this->data['Dvutsinhvien']['data'].'%'
                ))));
                if(!empty($results)){
                    $this->set('results', $results);    
                }else {$this->set('check', "No results!");}
                
            }
        }
    }
    /**
     * function duyetdvut($cdid=null)
     * The hien form duyet doan vien uu tu, cdid la id cua chi doan dang duyet 
     * */
    function duyetdvut($cdid=null){
       $namhocht_id=$this->Session->read('namhocht_id');
       $dvutsinhviens=$this->Z1dvutsinhvien->find('all', array('conditions'=> array('namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cdid)));
       $chidoan=$this->Chidoansv->find('first',array('conditions'=>array('Chidoansv.id'=>$cdid),'recursive'=>-1));
       $this->set(compact('dvutsinhviens','chidoan'));
    }
    /**
     * function duyet()
     * Duyet doan vien uu tu, co the duyet theo 1 hoac nhiu doan vien
     * cco 2 chuc nang: duyet, mo khoa 
     * */
    function duyet(){
        $namhocht_id=$this->Session->read('namhocht_id');
        //$cd=$this->Session->read('chidoanht_id');
        $cdid=$this->data['Dvutsinhvien']['chidoan_id'];
        $duyetids = array();              
		foreach($this->data['Dvutsinhvien']['duyet'] as $id => $value){
			if($value == 1){
	  		  $duyetids[] = $id;
			}
		}
        $chuaduyetids = array();              
		foreach($this->data['Dvutsinhvien']['chuaduyet'] as $id => $value){
			if($value == 1){
	  		  $chuaduyetids[] = $id;
			}
		}
		if (count($duyetids) == 0&&count($chuaduyetids) == 0) {
			$this->Session->setFlash(__('Khong co sinh vien nao duoc chon', true));
			//$this->redirect(array('action'=>'duyetdvut',$cdid));
            $this->redirect(array('controller'=>'phanloaisinhviens','action'=>'duyetcd',$cdid));
		}else{
            $this->Dvutsinhvien->updateAll(array('Dvutsinhvien.duyet'=>1),array('Dvutsinhvien.id'=>$duyetids));
            $this->Dvutsinhvien->updateAll(array('Dvutsinhvien.duyet'=>0),array('Dvutsinhvien.id'=>$chuaduyetids));
			$this->Session->setFlash(__('Đã Lưu !', true));
			//$this->redirect(array('action'=>'duyetdvut',$cdid));
            $this->redirect(array('controller'=>'phanloaisinhviens','action'=>'duyetcd',$cdid));
				
		}    
    }

	function view($id = null) {
	   
		if (!$id) {
			$this->Session->setFlash(__('Invalid Dvutsinhvien', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('dvutsinhviens', $this->Dvutsinhvien->read(null, $id));
	}

	function add() {
	   $data=$this->data;
       if (!empty($data)) {
            //for($j=1;$j<count($data['Dvutsinhvien'])/5;$j++){
                //echo $data['Dvutsinhvien']['sinhvien_id'.$j];
                //echo "soluong: ".count($data['Dvutsinhvien']);
                //$Dvutsinhvien[]=array('sinhvien_id'=>$data['Dvutsinhvien']['sinhvien_id'.$j],
//                                'hocky_id'=>$data['Dvutsinhvien']['hocky_id'.$j],
//                                'songuoidongy'=>$data['Dvutsinhvien']['songuoidongy'.$j],
//                                'songuoithamgiabau'=>$data['Dvutsinhvien']['songuoithamgiabau'.$j],
//                                'ghichu'=>$data['Dvutsinhvien']['ghichu'.$j]);
    			//$this->Dvutsinhvien->create();
    			$this->Dvutsinhvien->saveField('sinhvien_id',$data['Dvutsinhvien']['sinhvien_id1']);
   				$this->Dvutsinhvien->id=false;
    		//}
        }
        //$this->redirect(array('action' => 'index'));
		//$sinhviens = $this->Dvutsinhvien->Sinhvien->find('list');
		//$hockies = $this->Dvutsinhvien->Hocky->find('list');
		//$this->set(compact('sinhviens', 'hockies'));
	}
    //edit cho sv
    
    /**
     * function editdvut($id = null) 
     * edit dvut voi tham so dua vao la id cua sv
     * */
	function editdvut($id = null) {
	   $lock="";
	   $namhocht_id=$this->Session->read('namhocht_id');
       $cd=$this->Session->read('chidoanht_id');
       $sinhvien =$this->Z1dvutsinhvien->find('first', array('conditions'=> array('id'=>$id,'namhoc_id'=>$namhocht_id,'chidoansv_id'=>$cd)));
	   $this->set(compact('sinhvien'));
       if($sinhvien['Z1dvutsinhvien']['duyet']==0){
            if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Dvutsinhvien', true));
			$this->redirect(array('action' => 'viewdvut'));
    		}
    		if (!empty($this->data)) {
                
    			if ($this->Dvutsinhvien->save($this->data)) {
    				$this->Session->setFlash(__('The Dvutsinhvien has been saved', true));
    				$this->redirect(array('action' => 'viewdvut'));
    			} else {
    				$this->Session->setFlash(__('The Dvutsinhvien could not be saved. Please, try again.', true));
    			}
    		}
    		if (empty($this->data)) {
    			$this->data = $this->Dvutsinhvien->read(null, $id);
    		}
       }else{
            $this->Session->setFlash(__('Đã hết thời gian chỉnh sửa đoàn viên ưu tú', true));
            $this->set('lock',$lock);
       }
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Dvutsinhvien', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Dvutsinhvien->save($this->data)) {
				$this->Session->setFlash(__('The Dvutsinhvien has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Dvutsinhvien could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Dvutsinhvien->read(null, $id);
		}
		$sinhviens = $this->Dvutsinhvien->Sinhvien->find('list',array('fields'=>'mssv'));
		$hockies = $this->Dvutsinhvien->Hocky->find('list');
		$this->set(compact('sinhviens', 'hockies'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Dvutsinhvien', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Dvutsinhvien->delete($id)) {
			$this->Session->setFlash(__('Dvutsinhvien deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Dvutsinhvien was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
